import { IOption, IFormItem, FormItemType } from '@shared/interfaces/common'
import { numScale, compareTwoCurrencies } from 'app/utils/helpers'
import { WXChannel } from '@shared/interfaces/acquirer'
import { Validators } from '@angular/forms'
import {
  WeChatMerchantIDMaxLength,
  WeChatMerchantIDReg,
  TransactionFeeReg,
  ChannelMerchantNameMaxLength,
  ApiSecretMaxLength,
  ApiSecretMinLength,
  APiSecretReg,
  WeChatAppIDMaxLength,
  WeChatAppIDReg,
} from 'app/utils/contraints'

export const genWXFormItems = (currencyOptions: IOption[], handleChanMerIDChange): IFormItem[] => {
  return [
    {
      label: 'WeChat Merchant ID',
      errTips: {
        required: '必填项',
        existed: '该渠道已存在',
        maxLength: '最多不超过 32 位',
        pattern: '格式错误：只支持数字、字母',
      },
      formControlName: 'chanMerID',
      change: handleChanMerIDChange,
    },
    {
      label: 'API Certificate(CA issued)',
      errTip: '',
      type: FormItemType.FILE,
      formControlName: 'publicKey',
    },
    {
      label: 'API Certificate private key',
      errTip: '',
      type: FormItemType.FILE,
      formControlName: 'privateKeyArray',
    },
    {
      label: 'WeChat APP ID',
      errTips: {
        required: '必填项',
        maxLength: '最多不超过 32 位',
        pattern: '格式错误： 只支持数字字母',
      },
      formControlName: 'appID',
    },
    {
      label: 'API Secret',
      errTips: {
        required: '必填项',
        maxLength: '固定长度 32 位',
        minLength: '固定长度 32 位',
        pattern: '格式不正确：只支持数字、小数',
      },
      errTip: 'API Secret 输入不正确',
      formControlName: 'signKey',
    },
    {
      label: 'Transaction Currency',
      errTips: {
        required: '必填项',
      },
      formControlName: 'currencies',
      type: FormItemType.CURRENCY_SELECT,
      multiple: 'multiple',
      allowClear: true,
      options: currencyOptions,
      compareFn: compareTwoCurrencies,
    },
    {
      label: 'Transaction Fee',
      errTips: {
        required: '必填项',
        pattern: '格式不正确，最多只支持四位小数,且不可为负数',
      },
      errTip: 'Transaction Fee 只能在0-10%之间',
      type: FormItemType.INPUTGROUP,
      addAfter: '%',
      formControlName: 'fee',
      inputType: 'number',
    },
    {
      label: 'Channel Merchant Name',
      errTips: {
        required: '必填项',
        maxLength: '最多不超过 50 位',
      },
      formControlName: 'chanMerName',
    },
  ]
}

export const genWXValidateRules = (channel: WXChannel): { [key: string]: any } => {
  return {
    id: [null, []],
    chanCode: [channel && channel.chanCode, [Validators.required]],
    chanMerID: [
      channel && channel.chanMerID,
      [Validators.required, Validators.maxLength(WeChatMerchantIDMaxLength), Validators.pattern(WeChatMerchantIDReg)],
    ],
    currencies: [channel && channel.currencies, [Validators.required]],
    fee: [
      channel && numScale(channel.fee, 2),
      [Validators.required, Validators.min(0), Validators.max(10), Validators.pattern(TransactionFeeReg)],
    ],
    chanMerName: [
      channel && channel.chanMerName,
      [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
    ],
    signKey: [
      channel && channel.signKey,
      [
        Validators.required,
        Validators.maxLength(ApiSecretMaxLength),
        Validators.minLength(ApiSecretMinLength),
        Validators.pattern(APiSecretReg),
      ],
    ],
    publicKey: [channel && channel.publicKey, [Validators.required]],
    privateKeyArray: [channel && channel.privateKeyArray, [Validators.required]],
    appID: [
      channel && channel.appID,
      [Validators.required, Validators.maxLength(WeChatAppIDMaxLength), Validators.pattern(WeChatAppIDReg)],
    ],
    isLoaded: [channel && channel.isLoaded],
  }
}
